package com.softwinner;

import android.annotation.SuppressLint;

/**
 * GPIO控制JNI调用类
 * @author jack
 *
 */

@SuppressLint("DefaultLocale")
public class Gpio
{
	public static final String TAG = "GPIO";

    private Gpio(){
    	
    }

	static {
//		System.loadLibrary("gpio_jni");
//		nativeInit();
	}

	private static native void nativeInit();
    private static native int nativeWriteGpio(String path, String value);
	private static native int nativeReadGpio(String path);

	private static final String  mPathstr      = "/sys/class/gpio_sw/P";
	private static final String  mDataName     = "/data";
	private static final String  mPullName     = "/pull";
	private static final String  mDrvLevelName = "/drv_level";
	private static final String  mMulSelName   = "/mul_sel";

	public static int writeGpio(char group, int num, int value){
		return nativeWriteGpio(composePinPath(group, num).concat(mDataName), Integer.toString(value));
	}

	public static int readGpio(char group, int num){
		return nativeReadGpio(composePinPath(group, num).concat(mDataName));
	}

	public static int setPull(char group, int num, int value){
		return nativeWriteGpio(composePinPath(group, num).concat(mPullName), Integer.toString(value));
	}

	public static int getPull(char group, int num){
		return nativeReadGpio(composePinPath(group, num).concat(mPullName));
	}

	public static int setDrvLevel(char group, int num, int value){
		return nativeWriteGpio(composePinPath(group, num).concat(mDrvLevelName), Integer.toString(value));
	}

	public static int getDrvLevel(char group, int num){
		return nativeReadGpio(composePinPath(group, num).concat(mDrvLevelName));
	}

	public static int setMulSel(char group, int num, int value){
		return nativeWriteGpio(composePinPath(group, num).concat(mMulSelName), Integer.toString(value));
	}

	public static int getMulSel(char group, int num){
		return nativeReadGpio(composePinPath(group, num).concat(mMulSelName));
	}

	private static String composePinPath(char group, int num){
        return mPathstr.concat(String.valueOf(group).toUpperCase()).concat(Integer.toString(num));
    }
}

